//
//  CDH_PedometerViewController8.swift
//  Transducer
//
//  Created by chendehao on 16/8/20.
//  Copyright © 2016年 CDHao. All rights reserved.
//

import UIKit
import CoreMotion

class CDH_PedometerViewController8: UIViewController {
    
    @IBOutlet weak var stepCounterLabel: UILabel!
    lazy var stepPedometer : CMPedometer = CMPedometer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        stepCounterLabel.text = "您已经走了0步"
    }
}
extension CDH_PedometerViewController8{
    // 计步按钮
    @IBAction func stepCounterButtonClick(sender: UIButton) {
        
        // 1.判断计步器是否可用
        if !CMPedometer.isStepCountingAvailable() {
            stepCounterLabel.text = "您的计步器不可用, 可能是硬件故障, 可能是手机设置不允许本应用使用"
            return
        }
        
        // 2.开始计步
        stepPedometer.startPedometerUpdatesFromDate(NSDate()) {(data : CMPedometerData?, error : NSError?) in
            // 1.判断是否有错误
            if error != nil {
                self.stepCounterLabel.text = "error: \(error)"
                return
            }
            
            // 2.获取数据
            guard let data = data else {
                return
            }
 
            self.stepCounterLabel.text = "您一共走了:\(data.numberOfSteps)步"

            // \r 您一共走了: \(data.distance)米
        }
    }
}